#include <QApplication>
#include <QPlastiqueStyle>
#include <QFont>
#include <QTextStream>
#include <QDebug>

#include "yigscore.h"
#include "graphics/yigcolorkit.h"

YigScore::YigScore(QWidget *parent) :
    QWidget(parent),
    mScoreDirectory(QApplication::applicationDirPath() + "/synthdefs"),
    mCurrentScene(0)
{
    createScoreScenes();

    setWindowTitle("Yig Score");
    setGeometry(QRect(0, 0, 260, 182));
    setWindowFlags( Qt::Tool | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    //setFixedWidth(300);
    //setFixedHeight(75);
    setWindowOpacity(0.77);

    QStyle* yigStyle = new QMacStyle;
    QPalette yigPalette;
    yigPalette.setColor(QPalette::Window, YigColorKit::background2);
    yigPalette.setColor(QPalette::Button, YigColorKit::focus1);
    yigPalette.setColor(QPalette::ButtonText, YigColorKit::background2);
    setStyle(yigStyle);
    setPalette(yigPalette);

    mScoreDisplay.setStyle(yigStyle);
    mScoreDisplay.setReadOnly(true);
    mScoreDisplay.setGeometry(0, 0, 300, 60);
    QFont scoreFont("monaco", 10);
    mScoreDisplay.setFont(scoreFont);

    QPalette scorePalette = mScoreDisplay.palette();
    scorePalette.setColor(QPalette::Base, YigColorKit::background2);
    scorePalette.setColor(QPalette::Text, YigColorKit::focus1);
    scorePalette.setColor(QPalette::WindowText, YigColorKit::focus1);
    scorePalette.setColor(QPalette::Window, YigColorKit::background2);
    scorePalette.setColor(QPalette::Highlight, YigColorKit::background2);
    scorePalette.setColor(QPalette::Button, YigColorKit::focus2);
    scorePalette.setColor(QPalette::LinkVisited, YigColorKit::background2);
    mScoreDisplay.setPalette(scorePalette);

    mVerticalLayout.setSpacing(0);
    mVerticalLayout.setContentsMargins(0, 0, 0, 0);
    mVerticalLayout.addWidget(&mScoreDisplay);
    mVerticalLayout.addLayout(&mHorizontalLayout);

    mPrevButton.setText("<<");
    mPrevButton.setGeometry(0, 0, 50, 25);
    mPrevButton.setStyleSheet("QToolButton{ background: #222; color: #ddd; border-radius: 0px}"
                                "QToolButton::checked{ background: #bbb; color: #000; }"
                                "QToolButton::hover{ background: #bbb; color: #000; }");

    QObject::connect(&mPrevButton, SIGNAL(clicked()), this, SLOT(prevScene()));

    mNextButton.setText(">>");
    mNextButton.setGeometry(0, 0, 50, 25);
    mNextButton.setStyleSheet("QToolButton{ background: #222; color: #ddd; border-radius: 0px}"
                                "QToolButton::checked{ background: #bbb; color: #000; }"
                                "QToolButton::hover{ background: #bbb; color: #000; }");

    QObject::connect(&mNextButton, SIGNAL(clicked()), this, SLOT(nextScene()));

    mHorizontalLayout.setSpacing(10);
    mHorizontalLayout.setContentsMargins(0, 0, 10, 0);
    //mHorizontalLayout.setAlignment(Qt::AlignCenter);
    mHorizontalLayout.addWidget(&mClock);
    mHorizontalLayout.addWidget(&mPrevButton);
    mHorizontalLayout.addWidget(&mNextButton);

    setLayout(&mVerticalLayout);
    delete yigStyle;
}

QDir YigScore::getScoreDirectory()
{
    return mScoreDirectory;
}

void YigScore::setScoreDirectory(QDir scoreDirectory)
{
    mScoreDirectory = scoreDirectory;
}

void YigScore::createScoreScenes()
{
    if(!mScoreDirectory.exists())
    {
        qDebug("Cannot find the synthdefs directory. NosScore will be displayed.");
        mScoreDisplay.setText("COULD NOT FIND THE SYNTHDEFS DIRECTORY TO CREATE THE YIG SCORE.");
        return;
    }

    QStringList filters;
    filters << "*.yig";
    QStringList yigFileList = mScoreDirectory.entryList(filters);

    QString yigFilePath;

    if(yigFileList.size() < 1)
    {
        qDebug() << "Cannot find a .yig file in the current synthdefs directory. No score will be displayed.";
        mScoreDisplay.setText("NO YIG SCORE FOUND.");
        return;
    }

    else if(yigFileList.size() > 1)
    {
        qDebug() << "More than one .yig file found in the current synthdefs directory. First yig file chosen.";
    }

    yigFilePath = yigFileList.first();
    QFile yigFile(mScoreDirectory.absolutePath() + "/" + yigFilePath);

    qDebug() << "Yig File Path" << yigFilePath;
    if(!yigFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Unable to open the .yig file. No score will be displayed";
        mScoreDisplay.setText("COULD NOT OPEN YIG SCORE.");
        return;
    }

    int lineCount = 0;
    QString sceneString;
    QTextStream in(&yigFile);
    while(!in.atEnd())
    {
        QString line = in.readLine();
        if(line.startsWith("//") && lineCount > 0)
        {
            mScoreList.push_back(sceneString);
            sceneString.clear();
            sceneString.append(line + "\n");
        }

        else
        {
            sceneString.append(line + "\n");
        }

        if(in.atEnd())
        {
            sceneString.append(line + "\n");
            mScoreList.push_back(sceneString);
        }

        ++lineCount;
    }

    if(mScoreList.size() == 0)
    {
        qDebug() << "Score is empty. No score will be displayed";
        mScoreDisplay.setText("Score file is empty.");
    }

    mScoreDisplay.setText(mScoreList.at(0));
    mCurrentScene = 0;
}

void YigScore::setScene(int sceneNum)
{
    if(sceneNum >= 0 && sceneNum < mScoreList.size())
    {
        mCurrentScene = sceneNum;
        mScoreDisplay.setText(mScoreList.at(mCurrentScene));
    }
}

void YigScore::prevScene()
{
    if(mScoreList.size() > 0)
    {
        mCurrentScene -= 1;
        if(mCurrentScene < 0)
            mCurrentScene = mScoreList.size() - 1;
        mScoreDisplay.setText(mScoreList.at(mCurrentScene));
    }

    emit sceneChanged(mCurrentScene);
}

void YigScore::nextScene()
{
    if(mScoreList.size() > 0)
    {
        mCurrentScene = (mCurrentScene + 1) % mScoreList.size();
        mScoreDisplay.setText(mScoreList.at(mCurrentScene));
    }

    emit sceneChanged(mCurrentScene);
}
